﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using ExtendedMvc.Tests.Model;
using ExtendedMvc.Tests.Model.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ExtendedMvc.Tests {


	/// <summary>
	///This is a test class for GridTest and is intended
	///to contain all GridTest Unit Tests
	///</summary>
	[TestClass()]
	public class HtmlGridTest {

		private static DummyDataSource database;
		private static ModelViewPage<IEnumerable<Dummy>> view;

		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext { get; set; }

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		[ClassInitialize()]
		public static void MyClassInitialize(TestContext testContext) {
			database = new DummyDataSource();
			var list = database.Dummies.All().Take(1);
			view = MvcMockHelpers.FakeViewPage("Index", database.Dummies.All().Take(1));
			if (!RouteTable.Routes.Any()) {
				RouteTable.Routes.MapRoute(
					"Default", // Route name
					"{controller}/{action}/{id}", // URL with parameters
					new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
				);
			}
		}

		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup() {
		//}
		//
		//Use TestInitialize to run code before running each test
		[TestInitialize()]
		public void MyTestInitialize() {
		}

		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup() {
		//}
		//
		#endregion

		/// <summary>
		///A test for Grid Constructor
		///</summary>
		[TestMethod()]
		public void Grid_WithKeyName() {
			// Arrange
			var informedKeyName = "Id";

			// Act
			var grid = view.Html.Grid(informedKeyName);
			var expectedOutput =
@"<table id=""{0}""><tr><th><a href=""/Home/New"">New</a></th>
<th>BasicProperty</th>
<th>EmptyProperty</th>
<th>RequiredProperty</th>
<th>Labeled Property</th>
<th>SizedProperty</th>
<th>FormatedProperty</th>
<th>BoolProperty</th>
<th>EnumProperty</th>
</tr>
<tr><td><a href=""/Home/View?key=1"">View</a><a href=""/Home/Edit?key=1"">Edit</a><a href=""/Home/Delete?key=1"">Delete</a></td>
<td>Basic1</td>
<td></td>
<td>Required1</td>
<td>Labeled1</td>
<td>Sized1</td>
<td></td>
<td>False</td>
<td>FirstValue</td>
</tr>
</table>";
			var output = grid.ToString();

			// Assert
			Assert.AreEqual<string>(string.Format(expectedOutput, grid.Id), output);
		}

		/// <summary>
		///A test for Grid Constructor
		///</summary>
		[TestMethod()]
		public void Grid_WithNoData() {
			// Arrange
			var informedKeyName = "Id";
			var emptyview = MvcMockHelpers.FakeViewPage("Index", database.Dummies.All().Take(0));

			// Act
			var grid = emptyview.Html.Grid(informedKeyName);
			var expectedOutput =
@"<table id=""{0}""><tr><th><a href=""/Home/New"">New</a></th>
<th>BasicProperty</th>
<th>EmptyProperty</th>
<th>RequiredProperty</th>
<th>Labeled Property</th>
<th>SizedProperty</th>
<th>FormatedProperty</th>
<th>BoolProperty</th>
<th>EnumProperty</th>
</tr>
<tr><td colspan=""9"">no data</td></tr>
</table>";
			var output = grid.ToString();

			// Assert
			Assert.AreEqual<string>(string.Format(expectedOutput, grid.Id), output);
		}

		/// <summary>
		///A test for Grid Constructor
		///</summary>
		[TestMethod()]
		public void Grid_WithCustomColumns() {
			// Arrange
			var informedKeyName = "Id";

			// Act
			var grid = view.Html.Grid(informedKeyName).Columns(column => {
				column.ForCommands();
				column.ForData(m => m.BasicProperty);
				column.ForData(m => m.EmptyProperty);
				column.ForData(m => m.RequiredProperty);
			});
			var expectedOutput =
@"<table id=""{0}""><tr><th><a href=""/Home/New"">New</a></th>
<th>BasicProperty</th>
<th>EmptyProperty</th>
<th>RequiredProperty</th>
</tr>
<tr><td><a href=""/Home/View?key=1"">View</a><a href=""/Home/Edit?key=1"">Edit</a><a href=""/Home/Delete?key=1"">Delete</a></td>
<td>Basic1</td>
<td></td>
<td>Required1</td>
</tr>
</table>";
			var output = grid.ToString();

			// Assert
			Assert.AreEqual<string>(string.Format(expectedOutput, grid.Id), output);
		}

		/// <summary>
		///A test for Grid Constructor
		///</summary>
		[TestMethod()]
		public void Grid_WithCustomCommands() {
			// Arrange
			var informedKeyName = "Id";

			// Act
			var grid = view.Html.Grid(informedKeyName).Columns(column => {
				column.ForCommands(command => {
					command.ForHeader("Header Link", "~/dummy.aspx");
					command.ForHeaderAsAction("Header Action", "Index");
					command.ForHeaderAsRoute("Header Route", "Default");
					command.ForDataRow("Data Link", "~/dummy.aspx");
					command.ForDataRowAsAction("Data Action", "Index");
					command.ForDataRowAsRoute("Data Route", "Default");
				});
				column.ForData(m => m.BasicProperty);
				column.ForData(m => m.EmptyProperty);
				column.ForData(m => m.RequiredProperty);
			});
			var expectedOutput =
@"<table id=""{0}""><tr><th><a href=""/dummy.aspx"">Header Link</a>&nbsp;|&nbsp;<a href=""/"">Header Action</a>&nbsp;|&nbsp;<a href=""/"">Header Route</a></th>
<th>BasicProperty</th>
<th>EmptyProperty</th>
<th>RequiredProperty</th>
</tr>
<tr><td><a href=""/dummy.aspx"">Data Link</a><a href=""/?key=1"">Data Action</a><a href=""/?key=1"">Data Route</a></td>
<td>Basic1</td>
<td></td>
<td>Required1</td>
</tr>
</table>";
			var output = grid.ToString();

			// Assert
			Assert.AreEqual<string>(string.Format(expectedOutput, grid.Id), output);
		}

		/// <summary>
		///A test for Grid Constructor
		///</summary>
		[TestMethod()]
		public void Grid_OnClick() {
			// Arrange
			var informedKeyName = "Id";
			var onClickEvent = "return false;";

			// Act
			var grid = view.Html.Grid(informedKeyName).OnClick(onClickEvent);
			var expectedOutput =
@"<table id=""{0}"" onclick=""{1}""><tr><th><a href=""/Home/New"">New</a></th>
<th>BasicProperty</th>
<th>EmptyProperty</th>
<th>RequiredProperty</th>
<th>Labeled Property</th>
<th>SizedProperty</th>
<th>FormatedProperty</th>
<th>BoolProperty</th>
<th>EnumProperty</th>
</tr>
<tr><td><a href=""/Home/View?key=1"">View</a><a href=""/Home/Edit?key=1"">Edit</a><a href=""/Home/Delete?key=1"">Delete</a></td>
<td>Basic1</td>
<td></td>
<td>Required1</td>
<td>Labeled1</td>
<td>Sized1</td>
<td></td>
<td>False</td>
<td>FirstValue</td>
</tr>
</table>";
			var output = grid.ToString();

			// Assert
			Assert.AreEqual<string>(string.Format(expectedOutput, grid.Id, onClickEvent), output);
		}

		/// <summary>
		///A test for Grid Constructor
		///</summary>
		[TestMethod()]
		public void Grid_OnMouseOver() {
			// Arrange
			var informedKeyName = "Id";
			var onMouseOverEvent = "return false;";

			// Act
			var grid = view.Html.Grid(informedKeyName).OnMouseOver(onMouseOverEvent);
			var expectedOutput =
@"<table id=""{0}"" onmouseover=""{1}""><tr><th><a href=""/Home/New"">New</a></th>
<th>BasicProperty</th>
<th>EmptyProperty</th>
<th>RequiredProperty</th>
<th>Labeled Property</th>
<th>SizedProperty</th>
<th>FormatedProperty</th>
<th>BoolProperty</th>
<th>EnumProperty</th>
</tr>
<tr><td><a href=""/Home/View?key=1"">View</a><a href=""/Home/Edit?key=1"">Edit</a><a href=""/Home/Delete?key=1"">Delete</a></td>
<td>Basic1</td>
<td></td>
<td>Required1</td>
<td>Labeled1</td>
<td>Sized1</td>
<td></td>
<td>False</td>
<td>FirstValue</td>
</tr>
</table>";
			var output = grid.ToString();

			// Assert
			Assert.AreEqual<string>(string.Format(expectedOutput, grid.Id, onMouseOverEvent), output);
		}

		/// <summary>
		///A test for Grid Constructor
		///</summary>
		[TestMethod()]
		public void Grid_OnMouseOut() {
			// Arrange
			var informedKeyName = "Id";
			var onMouseOutEvent = "return false;";

			// Act
			var grid = view.Html.Grid(informedKeyName).OnMouseOut(onMouseOutEvent);
			var expectedOutput =
@"<table id=""{0}"" onmouseout=""{1}""><tr><th><a href=""/Home/New"">New</a></th>
<th>BasicProperty</th>
<th>EmptyProperty</th>
<th>RequiredProperty</th>
<th>Labeled Property</th>
<th>SizedProperty</th>
<th>FormatedProperty</th>
<th>BoolProperty</th>
<th>EnumProperty</th>
</tr>
<tr><td><a href=""/Home/View?key=1"">View</a><a href=""/Home/Edit?key=1"">Edit</a><a href=""/Home/Delete?key=1"">Delete</a></td>
<td>Basic1</td>
<td></td>
<td>Required1</td>
<td>Labeled1</td>
<td>Sized1</td>
<td></td>
<td>False</td>
<td>FirstValue</td>
</tr>
</table>";
			var output = grid.ToString();

			// Assert
			Assert.AreEqual<string>(string.Format(expectedOutput, grid.Id, onMouseOutEvent), output);
		}

		///// <summary>
		/////A test for Grid Constructor
		/////</summary>
		//[TestMethod()]
		//public void Grid_ForModel() {
		//    // Arrange
		//    var dummy = database.Dummies.All().First();
		//    var expectedOutput = string.Format("<span>{0}</span>", dummy.BasicProperty);

		//    // Act
		//    var grid = view.Html.Grid(m => m.BasicProperty);
		//    var output = grid.ToString();

		//    // Assert
		//    Assert.AreEqual<string>(expectedOutput, output);
		//}

		///// <summary>
		/////A test for Grid Constructor
		/////</summary>
		//[TestMethod()]
		//public void Grid_WithId() {
		//    // Arrange
		//    var informedContent = "Test";
		//    var informedId = "TestId";
		//    var expectedOutput = string.Format(@"<span id=""{1}"">{0}</span>", informedContent, informedId);

		//    // Act
		//    var grid = view.Html.Grid(informedContent, informedId);
		//    var output = grid.ToString();

		//    // Assert
		//    Assert.AreEqual<string>(expectedOutput, output);
		//}

		/////// <summary>
		///////A test for Grid Constructor
		///////</summary>
		////[TestMethod()]
		////public void Grid_WithFormat() {
		////    // Arrange
		////    var informedContent = "Test";
		////    var informedFormat = "New {0}";
		////    var expectedOutput = string.Format("<span>{0}</span>", string.Format(informedFormat, informedContent));

		////    // Act
		////    var grid = view.Html.Grid(informedContent).Format(informedFormat);
		////    var output = grid.ToString();

		////    // Assert
		////    Assert.AreEqual<string>(expectedOutput, output);
		////}

		///// <summary>
		/////A test for Grid Constructor
		/////</summary>
		//[TestMethod()]
		//public void Grid_WithClass() {
		//    // Arrange
		//    var informedContent = "Test";
		//    var informedClass = "TestClass";
		//    var expectedOutput = string.Format(@"<span class=""{1}"">{0}</span>", informedContent, informedClass);

		//    // Act
		//    var grid = view.Html.Grid(informedContent).CssClass(informedClass);
		//    var output = grid.ToString();

		//    // Assert
		//    Assert.AreEqual<string>(expectedOutput, output);
		//}

		///// <summary>
		/////A test for Grid Constructor
		/////</summary>
		//[TestMethod()]
		//public void Grid_WithTitle() {
		//    // Arrange
		//    var informedContent = "Test";
		//    var informedTitle = "Test Title";
		//    var expectedOutput = string.Format(@"<span title=""{1}"">{0}</span>", informedContent, informedTitle);

		//    // Act
		//    var grid = view.Html.Grid(informedContent).Title(informedTitle);
		//    var output = grid.ToString();

		//    // Assert
		//    Assert.AreEqual<string>(expectedOutput, output);
		//}

		///// <summary>
		/////A test for Grid Constructor
		/////</summary>
		//[TestMethod()]
		//public void Grid_WithStyles() {
		//    // Arrange
		//    var informedContent = "Test";
		//    Func<string, string>[] informedStyles = new Func<string, string>[] { margin_top => "10px", display => "block" };
		//    var informedStyleString = "margin-top:10px; display:block;";
		//    var expectedOutput = string.Format(@"<span style=""{1}"">{0}</span>", informedContent, informedStyleString);

		//    // Act
		//    var grid = view.Html.Grid(informedContent).Styles(informedStyles);
		//    var output = grid.ToString();

		//    // Assert
		//    Assert.AreEqual<string>(expectedOutput, output);
		//}

		///// <summary>
		/////A test for Grid Constructor
		/////</summary>
		//[TestMethod()]
		//public void Grid_WithAddBefore() {
		//    // Arrange
		//    var informedContent = "Test";
		//    var somethingToAdd = "Text to Add";
		//    var expectedOutput = string.Format(@"{1}<span>{0}</span>", informedContent, somethingToAdd);

		//    // Act
		//    var grid = view.Html.Grid(informedContent).AddBefore(somethingToAdd);
		//    var output = grid.ToString();

		//    // Assert
		//    Assert.AreEqual<string>(expectedOutput, output);
		//}

		///// <summary>
		/////A test for Grid Constructor
		/////</summary>
		//[TestMethod()]
		//public void Grid_WithAddAfter() {
		//    // Arrange
		//    var informedContent = "Test";
		//    var somethingToAdd = "Text to Add";
		//    var expectedOutput = string.Format(@"<span>{0}</span>{1}", informedContent, somethingToAdd);

		//    // Act
		//    var grid = view.Html.Grid(informedContent).AddAfter(somethingToAdd);
		//    var output = grid.ToString();

		//    // Assert
		//    Assert.AreEqual<string>(expectedOutput, output);
		//}

		///// <summary>
		/////A test for Grid Constructor
		/////</summary>
		//[TestMethod()]
		//public void Grid_UsingRender() {
		//    // Arrange
		//    var informedContent = "Test";
		//    var expectedOutput = string.Format(@"<span>{0}</span>", informedContent);

		//    // Act
		//    var grid = view.Html.Grid(informedContent);
		//    var output = grid.Render();

		//    // Assert
		//    Assert.IsInstanceOfType(output, typeof(MvcHtmlString));
		//    Assert.AreEqual<string>(expectedOutput, output.ToString());
		//}

		///// <summary>
		/////A test for Grid Constructor
		/////</summary>
		//[TestMethod()]
		//public void Grid_WithClick() {
		//    // Arrange
		//    var informedContent = "Test";
		//    var onClickEvent = "return false;";
		//    var expectedOutput = string.Format(@"<span onclick=""{1}"">{0}</span>", informedContent, onClickEvent);

		//    // Act
		//    var grid = view.Html.Grid(informedContent).OnClick(onClickEvent);
		//    var output = grid.ToString();

		//    // Assert
		//    Assert.AreEqual<string>(expectedOutput, output);
		//}
	}
}
